package com.tl.juc.c3;

public class C1_SerialTest {
    static C1_SerialTest serialTest;
    static Boolean isInit = false;

    public static void main(String[] args) throws InterruptedException {
        for (int i=0;i<1000000;i++){
            isInit = false;
            serialTest = null;
             // 线程1
            Thread thread1 = new Thread(() -> {
                synchronized (isInit) {
                    serialTest = new C1_SerialTest();
                    isInit = true;
                }
            });

            // 线程2
            Thread thread2 = new Thread(() -> {
                synchronized (isInit){
                    if (isInit) {
                        serialTest.doSomething();
                    }
                }
            });
            thread1.start();
            thread2.start();

            thread1.join();
            thread1.join();
        }
    }
    private void doSomething() {
        System.out.println("doSomething...");
    }
}
